package dal;

import entity.Book;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author NhatNK
 */
public class BookDAL {
    public void saveBooks(List<Book> books){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            Node root = doc.createElement("BookStore");
            doc.appendChild(root);
            for (int i = 0; i < books.size(); i++) {
                Node book = doc.createElement("Book");
                
                Node title = doc.createElement("Title");
                title.setTextContent(books.get(i).getTitle());
                book.appendChild(title);
                
                Node author = doc.createElement("Author");
                author.setTextContent(books.get(i).getAuthor());
                book.appendChild(author);
                
                Node price = doc.createElement("Price");
                price.setTextContent(books.get(i).getPrice() +"");
                book.appendChild(price);
                
                root.appendChild(book);
            }
            
            TransformerFactory transFac = TransformerFactory.newInstance();
            Transformer transformer = transFac.newTransformer();
            Source source = new DOMSource(doc);
            Result result = new StreamResult(new File("books.xml"));
            transformer.transform(source, result);
            System.out.println(new File("books.xml").getAbsolutePath());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public List<Book> loadBooks(){
        List<Book> books = new LinkedList<Book>();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("books.xml"));
            NodeList bookNodes = doc.getElementsByTagName("Book");
            
            for (int i = 0; i < bookNodes.getLength(); i++) {
                Node book = bookNodes.item(i);
                String title = ((Element)book).getElementsByTagName("Title").item(0).getTextContent();
                String author = ((Element)book).getElementsByTagName("Author").item(0).getTextContent();
                float price = Float.parseFloat(((Element)book).getElementsByTagName("Price").item(0).getTextContent());
                Book b = new Book(title, author, price);
                books.add(b);
                 
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return books;
    }
    
}
